package com.ruoyi.asset.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.asset.domain.SysExpendDetailsAsset;
import com.ruoyi.asset.mapper.SysExpendDetailsAssetMapper;
import com.ruoyi.asset.service.ISysExpendDetailsAssetService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.order.domain.SysOrderOne;
import com.ruoyi.order.mapper.SysOrderOneMapper;
import com.ruoyi.orderParent.domain.SysOrderParent;
import com.ruoyi.orderParent.mapper.SysOrderParentMapper;
import com.ruoyi.store.domain.SysStore;
import com.ruoyi.store.mapper.SysStoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * assetService业务层处理
 *
 * @author yangshuai
 * @date 2024-07-09
 */
@Service
public class SysExpendDetailsAssetServiceImpl implements ISysExpendDetailsAssetService
{
    @Autowired
    private SysExpendDetailsAssetMapper sysExpendDetailsMapper;


    @Autowired
    private SysStoreMapper storeMapper;

    @Autowired
    private SysOrderOneMapper orderOneMapper;

    @Autowired
    private SysOrderParentMapper orderParentMapper;
    /**
     * 查询asset
     *
     * @param id asset主键
     * @return asset
     */
    @Override
    public SysExpendDetailsAsset selectSysExpendDetailsById(Long id)
    {
        return sysExpendDetailsMapper.selectSysExpendDetailsById(id);
    }

    /**
     * 查询asset列表
     *
     * @param sysExpendDetails asset
     * @return asset
     */
    @Override
    public List<SysExpendDetailsAsset> selectSysExpendDetailsList(SysExpendDetailsAsset sysExpendDetails)
    {
        List<String> listType =new ArrayList<>();
        if (sysExpendDetails.getType()==null) {
            listType.add("1");
            listType.add("2");
            listType.add("3");
        }
        else if (sysExpendDetails.getType().equals("1")) {
            listType.add("1");
        }
        else if (sysExpendDetails.getType().equals("2")) {
            listType.add("2");
            listType.add("3");
        }

        sysExpendDetails.setTypeList(listType);

        //获取当前用户的的商户id
        Long deptId = SecurityUtils.getDeptId();
        List<SysStore> sysStores = storeMapper.selectSysStoreByDeptId(deptId);
        if (CollectionUtils.isEmpty(sysStores)) {
            throw new ServiceException("用户异常");
        }
        Long storeId = sysStores.get(0).getId();


        sysExpendDetails.setStoreId(storeId);

        startPage();
        List<SysExpendDetailsAsset> sysExpendDetailsAssets = sysExpendDetailsMapper.selectSysExpendDetailsList(sysExpendDetails);

        if (CollectionUtils.isEmpty(sysExpendDetailsAssets)) {
            return sysExpendDetailsAssets;
        }

        List<Long> oneOrderList = new ArrayList<>();
        List<Long> moreOrderList = new ArrayList<>();

        for (SysExpendDetailsAsset sysExpendDetailsAsset : sysExpendDetailsAssets) {
            String type = sysExpendDetailsAsset.getType();
            if (type.equals("2")) {
                if (sysExpendDetailsAsset.getDataId()!=null) {
                    oneOrderList.add(sysExpendDetailsAsset.getDataId());
                }
                continue;

            }
            if (type.equals("3")) {
                moreOrderList.add(sysExpendDetailsAsset.getDataId());
            }

        }

        //批量查询关联订单：
        if (oneOrderList.size()>0 || moreOrderList.size()>0) {
            Map<Long, String> oneOrderMap=new HashMap<>();
            if (oneOrderList.size()>0) {
                List<SysOrderOne> orderOnes = orderOneMapper.selectSysOrderOneByIds(oneOrderList);
                oneOrderMap = orderOnes.stream().collect(Collectors.toMap(SysOrderOne::getId, c -> c.getNumber()));

            }
            Map<Long, String> moreOrderMap=new HashMap<>();
            if (moreOrderList.size()>0) {
                List<SysOrderParent> orderParents= orderParentMapper.selectSysOrderParentByIds(moreOrderList);
                moreOrderMap = orderParents.stream().collect(Collectors.toMap(SysOrderParent::getId, c -> c.getNumber()));

            }

            for (SysExpendDetailsAsset sysExpendDetailsAsset : sysExpendDetailsAssets) {
                String type = sysExpendDetailsAsset.getType();
                if (type.equals("2")) {
                    if (sysExpendDetailsAsset.getDataId()!=null) {
                        if (oneOrderMap.containsKey(sysExpendDetailsAsset.getDataId())) {

                            sysExpendDetailsAsset.setRelateOrder("单人背调订单:"+oneOrderMap.get(sysExpendDetailsAsset.getDataId()));

                        }
                    }
                    continue;

                }
                if (type.equals("3")) {
                    if (sysExpendDetailsAsset.getDataId()!=null) {
                        if (moreOrderMap.containsKey(sysExpendDetailsAsset.getDataId())) {

                            sysExpendDetailsAsset.setRelateOrder("批量背调订单:"+moreOrderMap.get(sysExpendDetailsAsset.getDataId()));

                        }
                    }
                    continue;

                }
            }
        }


        return sysExpendDetailsAssets;
    }

    @Override
    public BigDecimal assetBalance(SysExpendDetailsAsset sysExpendDetailsAsset) {
        return sysExpendDetailsMapper.assetBalance(sysExpendDetailsAsset);
    }

    /**
     * 新增asset
     *
     * @param sysExpendDetails asset
     * @return 结果
     */
    @Override
    public int insertSysExpendDetails(SysExpendDetailsAsset sysExpendDetails)
    {
        sysExpendDetails.setCreateTime(DateUtils.getNowDate());
        return sysExpendDetailsMapper.insertSysExpendDetails(sysExpendDetails);
    }

    /**
     * 修改asset
     *
     * @param sysExpendDetails asset
     * @return 结果
     */
    @Override
    public int updateSysExpendDetails(SysExpendDetailsAsset sysExpendDetails)
    {
        sysExpendDetails.setUpdateTime(DateUtils.getNowDate());
        return sysExpendDetailsMapper.updateSysExpendDetails(sysExpendDetails);
    }

    /**
     * 批量删除asset
     *
     * @param ids 需要删除的asset主键
     * @return 结果
     */
    @Override
    public int deleteSysExpendDetailsByIds(Long[] ids)
    {
        return sysExpendDetailsMapper.deleteSysExpendDetailsByIds(ids);
    }

    /**
     * 删除asset信息
     *
     * @param id asset主键
     * @return 结果
     */
    @Override
    public int deleteSysExpendDetailsById(Long id)
    {
        return sysExpendDetailsMapper.deleteSysExpendDetailsById(id);
    }
}
